package com.jshoperxms.service.impl;

import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.statickey.TagWords;
import com.jshoperxms.dao.PromotionProductSeckillContentTDao;
import com.jshoperxms.dao.PromotionProductSeckillTDao;
import com.jshoperxms.entity.PromotionProductSeckillContentT;
import com.jshoperxms.entity.PromotionProductSeckillT;
import com.jshoperxms.service.PromotionProductSeckillTService;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by sdywcd on 16/12/20.
 * Des:
 */
@Service("promotionProductSeckillTService")
@Scope("prototype")
public class PromotionProductSeckillTServiceImpl extends BaseTServiceImpl<PromotionProductSeckillT> implements PromotionProductSeckillTService {


    @Resource
    private PromotionProductSeckillTDao promotionProductSeckillTDao;

    @Resource
    private PromotionProductSeckillContentTDao promotionProductSeckillContentTDao;


    /**
     * 删除秒杀主题和秒杀商品内容
     * @param ids
     */
    @Transactional(propagation= Propagation.REQUIRED)
    @Override
    public void delPromotionProductSeckillTransactional(String ids) {
        String ides[]= StringUtils.split(ids, TagWords.SPLITDOT);
        for(String s:ides){
            PromotionProductSeckillT bean=this.promotionProductSeckillTDao.findByPK(PromotionProductSeckillT.class,s);
            if(bean!=null){
                bean.setStatus(BaseEnums.DataUsingState.DEL.getState());
                bean.setUpdatetime(DateTools.getSystemTimeBi());
                bean.setVersiont(bean.getVersiont()+1);
                this.promotionProductSeckillTDao.update(bean);
                Criterion criterion= Restrictions.eq("promotionProductSeckillId",bean.getId());
                List<PromotionProductSeckillContentT> contentTList=this.promotionProductSeckillContentTDao.findByCriteria(PromotionProductSeckillContentT.class,criterion);
                if(!contentTList.isEmpty()){
                    for(PromotionProductSeckillContentT c:contentTList){
                        c.setStatus(BaseEnums.DataUsingState.DEL.getState());
                        c.setUpdatetime(DateTools.getSystemTimeBi());
                        c.setVersiont(c.getVersiont()+1);
                        this.promotionProductSeckillContentTDao.update(c);
                    }
                }
            }
        }
    }
}
